package com.sx.pipeline.wxapi;

/**
 * author : zhouyouqiang
 * e-mail : 1638473898@qq.com
 * date   : 2022-07-21 16:40
 * desc   :
 * version: 1.0
 */

/**
 * author : zhouyouqiang
 * e-mail : 1638473898@qq.com
 * date   : 2021/12/20 14:20
 * desc   :
 * version: 1.0
 */

import android.content.Context;
import android.widget.Toast;

import com.sx.mvvmlibs.utils.Logs;
import com.sx.pipeline.R;
import com.sx.pipeline.repos.http.res.WechatResponse;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by Administrator on 2017/5/23.
 * 支付类
 */

public class PayHelper {
    private static PayHelper mPayHelper = null;

    public static PayHelper getInstance() {
        if (mPayHelper == null) {
            mPayHelper = new PayHelper();
        }
        return mPayHelper;
    }

    public PayHelper() {

    }

    IWXAPI msgApi = null;

    public void WexPay(Context context, WechatResponse wechatResponse) {
        if (msgApi == null) {
            msgApi = WXAPIFactory.createWXAPI(context, null);
            msgApi.registerApp(context.getString(R.string.wx_app_id));// 将该app注册到微信
        }
        PayReq req = new PayReq();
        if (!msgApi.isWXAppInstalled()) {
            Toast.makeText(context, "手机中没有安装微信客户端!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (wechatResponse != null) {
            req.appId = wechatResponse.getAppid();
            req.partnerId = wechatResponse.getPartnerid();
            req.prepayId = wechatResponse.getPrepayid();
            req.nonceStr = wechatResponse.getNoncestr();
            req.timeStamp = wechatResponse.getTimestamp() + "";
            req.packageValue = "Sign=WXPay";
            req.sign = wechatResponse.getSign();
            req.extData = "app data";
            // req.extData = MaiLiApplication.getInstance().getUserInfo().getPhone();  微信拓展参数
            boolean value = msgApi.sendReq(req);
            Logs.d("wechatResponse "+value);

        }
    }

//    public void AliPay(Activity activity, final String orderInfo) {
//        final PayTask alipay = new PayTask(activity);
//        Runnable payRunnable = new Runnable() {
//
//            @Override
//            public void run() {
//
//                Map<String, String> result = alipay.payV2(orderInfo, true);
//                Log.i("msp", result.toString());
//                Message msg = new Message();
//                msg.what = 0;
//                msg.obj = result;
////                mHandler.sendMessage(msg);
//            }
//        };
//
//        Thread payThread = new Thread(payRunnable);
//        payThread.start();
//    }

    //    private Handler mHandler = new Handler() {
//        @SuppressLint("HandlerLeak")
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case 0:
//                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
//                    /**
//                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
//                     */
//                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
//                    String resultStatus = payResult.getResultStatus();
//                    // 判断resultStatus 为9000则代表支付成功
//                    if (TextUtils.equals(resultStatus, "9000")) {
//                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
//                        if (mIPayListener != null) {
//                            mIPayListener.onSuccess();
//                        }
//                    } else {
//                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
//                        if (mIPayListener != null) {
//                            mIPayListener.onFail();
//                        }
//                    }
//                    break;
//            }
//        }
//    };
    private IPayListener mIPayListener;

    public void setIPayListener(IPayListener mIPayListener) {
        this.mIPayListener = mIPayListener;
    }

    public interface IPayListener {
        void onSuccess();

        void onFail();
    }
}
